package com.op.teachingliteapp.sender.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;

import java.net.InetSocketAddress;

import android.util.Log;
import android.widget.ArrayAdapter;

import com.op.teachingliteapp.sender.ui.view.device.DeviceDisplay;
import com.op.teachingliteapp.sender.ui.view.device.DeviceScan;


/**
 * Created by Genji on 2015/5/16.
 */
public class DeviceScanHandler extends SimpleChannelInboundHandler<DatagramPacket> {
    private ArrayAdapter adapter;

    public DeviceScanHandler(ArrayAdapter adapter) {
        this.adapter = adapter;
    }

    @Override
    protected void messageReceived(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception {
        Log.i("messageReceived", "----------");
        InetSocketAddress inetSocketAddress = datagramPacket.sender();
        String deveiceName = datagramPacket.content().toString(CharsetUtil.UTF_8);
        String ip = inetSocketAddress.getAddress().getHostAddress();
        DeviceDisplay deviceDisplay = new DeviceDisplay(deveiceName, ip);
        Log.i("device", ip);
        new DeviceScan(adapter, deviceDisplay).execute();

    }
}
